package com.gxa.employment.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

/**
 * @Description:
 * @Author:
 * @DateTime: 2025/1/19 15:19
 **/
@WebFilter(urlPatterns = "/*")
public class CharacterFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //设置请求的字符编码
        servletRequest.setCharacterEncoding("UTF-8");

        //设置响应的字符编码
        servletResponse.setCharacterEncoding("UTF-8");
        //设置响应内容(Result)的类型
        servletResponse.setContentType("application/json; charset=UTF-8");
        //放行
        filterChain.doFilter(servletRequest, servletResponse);

        /*
        如果还所有都执行完了才进行设置Result内容的类型、编码，不会生效，而是按照默认的类型进行封装
        //设置响应的字符编码
        servletResponse.setCharacterEncoding("UTF-8");
        //设置响应内容(Result)的类型
        servletResponse.setContentType("application/json; charset=UTF-8");
        */
    }
}
